//导入接口
import {topCategory} from '@/api/constants'
import {findAllCategory} from '@/api/category'


//导航分类
export default {
    namespaced:true,
    actions: {
        //获取list的方法
        async getList({commit},value) {
            const {result} = await findAllCategory()
            //添加控制跳转后关闭二级分类弹窗
            result.forEach((item) => {
                item.open = false
            })
            commit('setList',result)
        }
    },
    mutations: {
        //修改list的方法
        setList(state,headCategory) {
            state.list = headCategory
        },

        // 修改当前一级分类下的open数据为true
        show(state,value) {
            const category = state.list.find(item => item.id === value.id)
            category.open = true
        },

        // 修改当前一级分类下的open数据为false
        hide(state,value) {
            const category = state.list.find(item => item.id === value.id)
            category.open = false
        }
    },
    state:() => ({
        // 分类信息集合 依赖topCategory重新设置，保证初始就要有数据 不至于刚刚加载时空白
        list:topCategory.map(item => ({ name:item }))
    }),
    getters: {

    }
}